<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'btn:add:gongchenghttong'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增合同') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.proname }}
        </a>
      </template>

      <template #projectno="{ record }">
        <!-- <a @click="handleDetailsForm(record)" >
          <span style="text-decoration: underline;margin-right: 5px;">查看</span>
        </a>
        {{ record.prono }} -->
        <a @click="handleDetailsForm(record)" >
          {{ record.prono }}
        </a>
        
      </template>


    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProjContractPmContract2List',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted, ref, watch  } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { pmContract2Delete, pmContract2ListData } from '/@/api/proj/contract/pmContract2';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { useUserStore } from '/@/store/modules/user';
  import { customListData } from '/@/api/test/testData';
  import { listSet } from '/@/api/test/testData';
  import { schemasData, tabConfigData } from '/@/utils/custom';
  const userStore = useUserStore();

  const { t } = useI18n('proj.contract.pmContract2');
  const { showMessage } = useMessage();

  const props = defineProps({
    projectno: { type: String, default: '' },
  });

  //配置表单内容
  const searchForm = ref<FormProps>({
    baseColProps: { lg: 6, md: 8 }, // 表单栅格布局
    labelWidth: 90, // 表单标签宽度
    schemas: [],
  });
  let tableColumns = ref<any>([]);
  let butData = ref<any>([]);
  let actionColumnData = ref<ActionItem[]>([]);
  let layVoucherView = ref<any>({});
  let listTabConfig = ref<any>({});
  let title = ref('');

  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    // value: router.currentRoute.value.meta.title || t('工程项目合同管理'),
    value: t('工程项目合同管理'),
  };

  onMounted(async () => {
    const res = await listSet({ viewCode: 'zfgs_pmContract2_list001' });
    title.value = res.layVoucherView.name;
    layVoucherView.value = res.layVoucherView;

    searchForm.value.schemas = schemasData(layVoucherView.value.listQuery.queryCols);

    tableColumns.value = layVoucherView.value.flistTabCols; //表格表头

    butData.value = layVoucherView.value.headBtns; //表格右上角按钮

    actionColumnData.value = layVoucherView.value.rowBtns; //表格行内按钮

    listTabConfig.value = tabConfigData(res.layVoucherView.listTabConfig);
    await setProps({
      api: customListData,
      beforeFetch: (params) => {
        // console.log(listTabConfig.value.api);
        params.url = listTabConfig.value.api;
        // console.log(params, '123456'); //查询列表参数信息
        return params;
      },
      autoCreateKey: listTabConfig.value.autoCreateKey,
      bordered: listTabConfig.value.bordered,
      canResize: listTabConfig.value.canResize,
      clearSelectOnPageChange: listTabConfig.value.clearSelectOnPageChange,
      clickToRowSelect: listTabConfig.value.clickToRowSelect,
      ellipsis: listTabConfig.value.ellipsis,
      emptyDataIsShowTable: listTabConfig.value.emptyDataIsShowTable,
      inset: listTabConfig.value.inset,
      isTreeTable: listTabConfig.value.isTreeTable,
      pagination: listTabConfig.value.pagination,
      showIndexColumn: listTabConfig.value.showIndexColumn,
      showSummary: listTabConfig.value.showSummary,
      showTableSetting: listTabConfig.value.showTableSetting,
      striped: listTabConfig.value.striped,
      useSearchForm: listTabConfig.value.useSearchForm,
      tableSetting: listTabConfig.value.tableSetting,
      titleHelpMessage: listTabConfig.value.titleHelpMessage,
      maxHeight: listTabConfig.value.maxHeight,
      resizeHeightOffset: listTabConfig.value.resizeHeightOffset,
    });
    // await getForm().setFieldsValue({
    //   prono: userStore.getProjecte.code,
    // });
    // if (!listTabConfig.value.immediate) {
    //   reload();
    // }
    handleProjecte();
  });

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑工程项目合同'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'proj:contract:pmContract2:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除工程项目合同'),
        popConfirm: {
          title: t('是否确认删除工程项目合同'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'btn:delete:gongchenghttong',
      },
    ],
  };

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        id: record.projInfo.id,
        code: record.prono,
        tabTitle: `项目详情(${record.prono})`,
      },
    });
  }

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, setProps }] = useTable({
    actionColumn: actionColumn,
    columns: tableColumns,
    formConfig: searchForm.value as FormProps,
  });
  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await pmContract2Delete(record);
    showMessage(res.message);
    handleSuccess();
  }
  watch(
    () => props.projectno,
    () => {
      handleProjecte();
    },
  );

  function handleSuccess() {
    reload();
  }

  async function handleProjecte() {
    await getForm().setFieldsValue({
      prono: props.projectno?props.projectno:userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      prono: props.projectno?props.projectno:userStore.getProjecte.code,
    });
  }
</script>
